package com.miaowang.salad.jobqueue;

import org.json.JSONObject;

import android.webkit.URLUtil;

import com.alibaba.fastjson.JSON;
import com.miaowang.salad.data.OkHttpResponseData;
import com.miaowang.salad.net.OkHttpUtils;
import com.miaowang.salad.net.UrlUtils;
import com.path.android.jobqueue.Job;
import com.path.android.jobqueue.Params;
import com.path.android.jobqueue.RetryConstraint;

import de.greenrobot.event.EventBus;

/**
 * 获取救援工当前位置
 * @author weihe
 *
 */
public class GetRescueLocationJob extends Job {

	
	private String id;
	public GetRescueLocationJob(String id ) {
		super(new Params(JobQueuePriority.PRIORITY_HIGH).groupBy(JobQueuePriority.JOBS_GROUP_USER).requireNetwork());
		this.id = id;
	}

	@Override
	public void onAdded() {

	}

	@Override
	protected void onCancel() {

	}

	@Override
	public void onRun() throws Throwable {
		JSONObject json = new JSONObject();
		json.put("id", id);
		String result = OkHttpUtils.post(UrlUtils.GET_RESCUE_LOCAITON,json.toString());
        OkHttpResponseData responseData = JSON.parseObject(result, OkHttpResponseData.class);
        EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_RESCUE_LOCATION,responseData));
	}

	@Override
	protected RetryConstraint shouldReRunOnThrowable(Throwable throwable,
			int runCount, int maxRunCount) {
		EventBus.getDefault().post(new JobEvents.JobSuccess<>(JobEvents.GET_RESCUE_LOCATION));
		return super.shouldReRunOnThrowable(throwable, runCount, maxRunCount);
	}
	
	

}
